const color = /^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/
let c1 = '#baf'
let c2 = '#012334'
let c3 = '123213'
console.log(color.test(c3))
//? 邮箱 任意字符或连字符开头 一个@接任意字符 一个或多个.接任意字符
const mail = /^[\w-]+@[\w-]+(\.[\w-]+)+$/
let mail1 = '1376736719@qq.com'
let mail2 = 's211231053@student.edu.cqupt.cn'
let mail3 = 's211231-053@student.edu.cqupt.cn'
let mail4 = 's211231-053@student.edu@cqupt.cn'
console.log(mail.test(mail3))
//? 逆扁平 匹配非.[]和[]内的值
const unFlatten = /\.?([^\.\[\]]+)|\[(\d+)\]/g
const v1 = "e.k[3].d"
console.log(unFlatten.exec(v1))
console.log(unFlatten.exec(v1))
console.log(unFlatten.exec(v1))
console.log(unFlatten.exec(v1))
//? 模板字符串 ${} $需要转义而{}不需要
const regex = /\${(\w+)}/g
const template = 'My name is ${name} and I am ${age} years old.';
const values = { name: 'Alice', age: 25 };
const result = template.replace(regex, (match, key) => {
    console.log('match test',match);
    return values[key]
});
console.log(result);
//? vue模板字符串 {{}}
const regexv = /{{(\w+)}}/g
const templatev = 'My name is {{name}} and I am {{age}} years old.';
const valuesv = { name: 'Alice', age: 25 };
const resultv = templatev.replace(regexv, (match, key) => {
    console.log(match);
    return valuesv[key]
});
console.log(result);
//? 换行符、制表符、回车符
const nrt = /[\n\t\r]/
const test = "\r\n\r\n\r\n"
console.log(nrt.test(test))
//? 函数体是否为空判断 只判断以}结尾的{}内的字符
function emptyFunction() {}
function nonEmptyFunction() {
    console.log("This function is not empty.");
}
const isNull = /{}$/
console.log(isNull.test(nonEmptyFunction.toString().replace(/\s/g, '')));
console.log(isNull.test(emptyFunction.toString().replace(/\s/g, '')));
//? 千位分隔数字
function parseToMoney(num) {
    num = parseFloat(num.toFixed(3));
    let [integer, decimal] = String.prototype.split.call(num, '.');
    //?  \d ：匹配一个数字。
    //?  (?=(\d{3})+$) ：正向预查，表示要匹配的数字后面必须跟着一组由3个数字组成的重复模式，并且这个重复模式可以出现一次或多次，即匹配千位分隔符的位置
    //? 替换字符串 '$&,' 中的 $& 表示匹配到的部分，逗号表示要插入的分隔符
    integer = integer.replace(/\d(?=(\d{3})+$)/g, '$&,');
    return decimal ? integer + '.' + decimal : integer;
}
//? 保留三位小数
console.log(parseToMoney(1234.56)); // return '1,234.56'
console.log(parseToMoney(123456789)); // return '123,456,789'
console.log(parseToMoney(1087654.321)); // return '1,087,654.321'
//? 千位分隔数字 小数也分隔版本
let num = 1234567.8923;
let str = num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
console.log(str); // "1,234,567.892,3"
//? 正则实现字符串的trim
//? 正则实现HTTP/HTTPS网址是否符合
